home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / DBUGDEMO.PAK / DBUGDEMO.BUG next >
Text File  |  1997-05-06  |  15KB  |  461 lines

  1. /**********************************************************************
  2.  *          Copyright (c) 1993 by Borland International, Inc.         *
  3.  *                                                                    *
  4.  *                           DBUGDEMO.C                                *
  5.  *                                                                    *
  6.  *    This is a buggy version of Turbo Debugger's example program.    *
  7.  *                                                                    *
  8.  **********************************************************************/
  9.  
  10. #define STRICT
  11.  
  12. #include <windows.h>
  13. #include "dbugdemo.h"
  14.  
  15. //   FUNCTION PROTOTYPES
  16.  
  17. int  DoWMCommand(WPARAM wParam, HWND hWnd);
  18. void DoLButtonDown(HWND hWnd, LONG lParam);
  19. void DoLButtonUp(HWND hWnd, LONG lParam);
  20. void DoMouseMove(HWND hWnd, LONG lParam);
  21. void DoPaint(HWND hWnd);
  22. void DrawShape(HDC hdc, int x, int y, int x2, int y2, int Shape,
  23.                int PenWidth, COLORREF PenColor, int Slope);
  24. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  25.  
  26. //   GLOBAL VARIABLES
  27.  
  28. #define ShapeI 100
  29.  
  30. int ShapeNumber = -1;           // Indicates the number of shapes drawn.
  31.  
  32. int CurrentShape = LINE;        // The shape the user is drawing.
  33.  
  34. int PenWidth = 3;               // The current pen width.
  35.                                 // Default width is medium.
  36.  
  37. COLORREF PenColor = RGB(255, 0, 0);    // The current pen color.
  38.                                        // Default is red.
  39.  
  40. typedef struct SSHAPE           // Struct which tracks the drawn shapes.
  41. {
  42.         RECT       Points;      // Location of shape.
  43.         int        PenWidth;    // Pen width for the shape.
  44.         int        Shape;       // Type of shape.
  45.           COLORREF   PenColor;    // Color of shape.
  46.         int        Slope;       // Used to draw lines correctly.
  47. } SHAPE;
  48.  
  49. SHAPE Shapes[ShapeI];           // Array that stores the shapes.
  50.  
  51. /*******************************************************
  52.  * function WinMain
  53.  *******************************************************/
  54. #pragma argsused
  55. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  56.                    LPSTR lpszCmdLine, int nCmdShow)
  57. {
  58.     WNDCLASS    wndClass;
  59.     MSG         msg;
  60.     HWND        hWnd;
  61.  
  62.     /*
  63.       * Register window class style if first
  64.      * instance of this program.
  65.      */
  66.     if (!hPrevInstance)
  67.     {
  68.         wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  69.         wndClass.lpfnWndProc    = WndProc;
  70.         wndClass.cbClsExtra     = 0;
  71.         wndClass.cbWndExtra     = 0;
  72.         wndClass.hInstance      = hInstance;
  73.           wndClass.hIcon          = LoadIcon(hInstance, "IDI_SIMPLEPAINT");
  74.         wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  75.         wndClass.hbrBackground  = GetStockObject(WHITE_BRUSH);
  76.         wndClass.lpszMenuName   = szAppName;
  77.         wndClass.lpszClassName  = szAppName;
  78.  
  79.         if (!RegisterClass(&wndClass))
  80.             return FALSE;
  81.      }
  82.  
  83.     /*
  84.      * Create and display the window.
  85.      */
  86.     hWnd = CreateWindow(szAppName, "Simple Paint",
  87.                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,
  88.                CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  89.  
  90.     ShowWindow(hWnd, nCmdShow);
  91.      UpdateWindow(hWnd);
  92.  
  93.     while (GetMessage(&msg, NULL, 0, 0))
  94.         TranslateMessage(&msg);
  95.         DispatchMessage(&msg);
  96.     return 0;
  97. }
  98.  
  99. /*******************************************************
  100.  * function WndProc
  101.  *    Handles all messages received by the window
  102.  *******************************************************/
  103. LRESULT CALLBACK WndProc (HWND hWnd, UINT Message,
  104.                           WPARAM wParam, LPARAM lParam)
  105. {
  106.     switch(Message)
  107.     {
  108.         case WM_COMMAND:
  109.                 return DoWMCommand(wParam, hWnd);
  110.  
  111.         case WM_LBUTTONDOWN:
  112.             DoLButtonDown(hWnd, lParam);
  113.             break;
  114.  
  115.         case WM_LBUTTONUP:
  116.             DoLButtonUp(hWnd, lParam);
  117.                 break;
  118.  
  119.         case WM_MOUSEMOVE:
  120.             DoMouseMove(hWnd, lParam);
  121.             break;
  122.  
  123.         case WM_PAINT:
  124.             DoPaint(hWnd);
  125.             break;
  126.  
  127.           case WM_DESTROY:
  128.             PostQuitMessage(0);
  129.                 break;
  130.  
  131.           default:
  132.                 return DefWindowProc(hWnd, Message, wParam, lParam);
  133.      }
  134.      return 0;
  135. }
  136.  
  137. /*****************************************************************
  138.  * function DrawShape
  139.  *    Draws the shape given by Shape parameter using PenWidth
  140.  *    and PenColor in the rectangle bounded by x,y,x2,y2. The
  141.  *    Slope parameter is used with LINE shapes to determine if
  142.  *    lines should be drawn with a negative or positive slope.
  143.  *****************************************************************/
  144. #pragma argsused
  145. void DrawShape(HDC hdc, int x, int y, int x2, int y2, int Shape,
  146.                     int PenWidth, COLORREF PenColor, int Slope)
  147. {
  148.      HANDLE      saveObject;
  149.  
  150.      /*
  151.       * Create the proper pen for this shape. Save the
  152.       * previously selected object from this DC.
  153.       */
  154.      saveObject = SelectObject(hdc, CreatePen(PS_SOLID,
  155.                                         PenWidth, PenColor));
  156.      switch(Shape)
  157.      {
  158.           case LINE:
  159.                 /* If Slope is set, then draw from LowerRight to UpperLeft. If
  160.                  * Slope is not set then draw from UpperLeft to LowerRight.
  161.                  */
  162.                 if (Slope = 1)
  163.                 {
  164.                      MoveToEx(hdc, x, y2, NULL);
  165.                      LineTo(hdc, x2, y);
  166.                 }
  167.                 else 
  168.                 {
  169.                      MoveToEx(hdc, x, y, NULL);
  170.                      LineTo(hdc, x2, y2)
  171.                 }
  172.                 break;
  173.  
  174.           case ELLIPSE:
  175.                 Ellipse(hdc, x, y, x2, y2);
  176.                 break;
  177.  
  178.           case RECTANGLE:
  179.                 Rectangle(hdc, x, y, x2, y2);
  180.                 break;
  181.      }
  182.  
  183.      /*
  184.       * Delete the currently selected object (the pen) and select whatever
  185.       * object was selected when we entered this routine.
  186.       */
  187.  
  188.      DeleteObject(SelectObject(hdc, saveObject));
  189. }
  190.  
  191. /****************************************************************
  192.  * function DoPaint
  193.  *    Processes WM_PAINT messages. WM_PAINT is generated
  194.  *    whenever UpdateWindow is called or another window is moved,
  195.  *    revealing a portion of the window receiving this message.
  196.  ****************************************************************/
  197. void DoPaint(HWND hWnd)
  198. {
  199.     int           i;
  200.     HDC           hdc, hMemDC;
  201.     RECT          theRect, destRect;
  202.     HBITMAP       theBitmap;
  203.     PAINTSTRUCT   ps;
  204.  
  205.     hdc = BeginPaint(hWnd, &ps);
  206.  
  207.     if (ShapeNumber >= 0)
  208.     {
  209.         /*
  210.          * Determine which rectangle on the window is invalid.
  211.          * If no rectangle is marked invalid, it will be a full
  212.          * window repaint.
  213.             */
  214.         GetUpdateRect(hWnd, &theRect, 0);
  215.         if (IsRectEmpty(&theRect))
  216.                 GetClientRect(hWnd, &theRect);
  217.  
  218.         /*
  219.          * Create a memory DC and bitmap the same
  220.          * size as the update rectangle.
  221.          */
  222.         hMemDC = CreateCompatibleDC(hdc);
  223.         theBitmap = CreateCompatibleBitmap(hdc,
  224.                          theRect.right-theRect.left,
  225.                          theRect.bottom-theRect.top);
  226.         SelectObject(hMemDC, theBitmap);
  227.  
  228.         /*
  229.          * Erase the memBitmap.
  230.          */
  231.           BitBlt(hMemDC, 0, 0,
  232.                theRect.right-theRect.left,
  233.                theRect.bottom-theRect.top,
  234.                     hdc, 0, 0, SRCCOPY);
  235.  
  236.         /*
  237.          * Draw only those shapes that lie
  238.          * within the update rectangle.
  239.          */
  240.         for (i = 0; i < ShapeNumber; ++i)
  241.         {
  242.             IntersectRect(&destRect, &Shapes[i].Points, &theRect);
  243.             if (!IsRectEmpty(&destRect))
  244.                 DrawShape(hMemDC,
  245.                     Shapes[i].Points.left-theRect.left,
  246.                     Shapes[i].Points.top-theRect.top,
  247.                     Shapes[i].Points.right-theRect.left,
  248.                     Shapes[i].Points.bottom-theRect.top,
  249.                           Shapes[i].Shape,
  250.                     Shapes[i].PenWidth,
  251.                     Shapes[i].PenColor,
  252.                           Shapes[i].Slope);
  253.             /*
  254.              * Note that when drawing the shape, the shape's
  255.              * position was transformed so that the origin was
  256.              * at the upper-left corner of the update rectangle.
  257.              * This is the point (0,0) on the bitmap that will
  258.              * map onto (theRect.left,theRect.top).
  259.              */
  260.         }
  261.  
  262.         /*
  263.          * Finally, copy the bitmap onto the update rectangle.
  264.          */
  265.         BitBlt(hdc, theRect.left, theRect.top,
  266.                theRect.right-theRect.left,
  267.                     theRect.bottom-theRect.top,
  268.                hMemDC, 0, 0, SRCCOPY);
  269.  
  270.           DeleteDC(hMemDC);
  271.         DeleteObject(theBitmap);
  272.     }
  273.     EndPaint(hWnd, &ps);
  274. }
  275.  
  276. /**********************************************************
  277.  * static variables oldx, oldy, mouseDown
  278.  *    Used to maintain both the state of the mouse position
  279.  *    and the button status between mouse messages.
  280.  **********************************************************/
  281.  
  282. static  oldx = -1, oldy = -1, mouseDown = 0;
  283.  
  284. /******************************************************************
  285.  * function DoLButtonDown
  286.  *    When the left button on the mouse is pressed, this routine
  287.  *    saves the origin of this shape, the current pen parameters,
  288.  *    and the current shape into the shapes array. The mouse
  289.  *    button is also marked as pressed.
  290.  ******************************************************************/
  291. void DoLButtonDown(HWND hWnd, LONG lParam)
  292. {
  293.     /*
  294.      * Redirect all subsequent mouse movements to this
  295.      * window until the mouse button is released.
  296.      */
  297.     SetCapture(hWnd);
  298.     oldy = Shapes[++ShapeNumber].Points.top = HIWORD(lParam);
  299.     oldx = Shapes[ShapeNumber].Points.left = LOWORD(lParam);
  300.     Shapes[ShapeNumber].Shape = CurrentShape;
  301.     Shapes[ShapeNumber].PenWidth = PenWidth;
  302.     Shapes[ShapeNumber].PenColor = PenColor;
  303.  
  304.     mouseDown = 1;
  305. }
  306.  
  307. /******************************************************************
  308.  * function DoLButtonUp
  309.  *    When the Left mouse button is released, this routine
  310.  *    allows other windows to receive mouse messages and saves
  311.  *    the position of the mouse as the other corner of a bounding
  312.  *    rectangle for the shape.
  313.  ******************************************************************/
  314. void DoLButtonUp(HWND hWnd, LONG lParam)
  315. {
  316.     if (mouseDown == 0)
  317.         return;
  318.  
  319.     ReleaseCapture();
  320.  
  321.      /*
  322.      * For rectangles to work with the IntersectRect function,
  323.      * they must be stored as left, top, right, bottom.
  324.       */
  325.  
  326.     SetRect(&Shapes[ShapeNumber].Points,
  327.         min(Shapes[ShapeNumber].Points.left, LOWORD(lParam)),
  328.         min(Shapes[ShapeNumber].Points.top, HIWORD(lParam)),
  329.         max(Shapes[ShapeNumber].Points.left, LOWORD(lParam)),
  330.         max(Shapes[ShapeNumber].Points.top, HIWORD(lParam)));
  331.  
  332.     /*
  333.      * If slope is decreasing, set equal to 0, then draw from UpperLeft
  334.      * to LowerRight. If slope is increasing, set equal to 1, and draw
  335.      * draw from LowerLeft to UpperRight.
  336.      */
  337.  
  338.     if (CurrentShape == LINE)
  339.           Shapes[ShapeNumber].Slope = ((Shapes[ShapeNumber].Points.left ==
  340.                 LOWORD(lParam)) ^ (Shapes[ShapeNumber].Points.top ==
  341.                 HIWORD(lParam))) ?  0 : 1;
  342.  
  343.           /*
  344.             * This statement uses an exclusive-or because the following
  345.             * is true for a line drawn from LowerRight to UpperLeft:
  346.             *     ((Shapes[ShapeNumber].Points.left == LOWORD) &&
  347.             *         (Shapes[ShapeNumber].Points.right == HIWORD)).
  348.             */
  349.  
  350.     /*
  351.      * Mark this region on the window as needing redrawing and force an 
  352.      * update.
  353.      */
  354.  
  355.     InvalidateRect(hWnd, &Shapes[ShapeNumber].Points, 0);
  356.     UpdateWindow(hWnd);
  357.      mouseDown = 0;
  358.     oldx = -1;
  359.     oldy = -1;
  360. }
  361.  
  362. static int SaveROP;
  363.  
  364. /**********************************************************************
  365.  * function DoMouseMove
  366.  *    When the mouse is moved and the button is down, this function
  367.  *    draws the current shape. It uses the Raster Operation NOTXORPEN
  368.  *    to draw the shape. When this mode is used, drawing the
  369.  *    same image twice returns the image to its original state.
  370.  *    NOTXORPEN turns black on black white, black on white black
  371.  *    and white on white white.
  372.  **********************************************************************/
  373. void DoMouseMove(HWND hWnd, LONG lParam)
  374. {
  375.      HDC hdc;
  376.  
  377.     if (mouseDown)
  378.     {
  379.         hdc = GetDC(hWnd);
  380.         /*
  381.          * Erase the old shape as the mouse is moved.
  382.          */
  383.         SaveROP = SetROP2(hdc, R2_NOTXORPEN);
  384.         DrawShape(hdc, Shapes[ShapeNumber].Points.left,
  385.                   Shapes[ShapeNumber].Points.top, oldx, oldy,
  386.                   Shapes[ShapeNumber].Shape,
  387.                   Shapes[ShapeNumber].PenWidth,
  388.                   Shapes[ShapeNumber].PenColor,
  389.                   0);
  390.         /*
  391.          * Draw the new shape to the new mouse position.
  392.          */
  393.           oldy = LOWORD(lParam);
  394.         oldx = HIWORD(lParam);
  395.         DrawShape(hdc, Shapes[ShapeNumber].Points.left,
  396.                   Shapes[ShapeNumber].Points.top, oldx, oldy,
  397.                   Shapes[ShapeNumber].Shape,
  398.                   Shapes[ShapeNumber].PenWidth,
  399.                   Shapes[ShapeNumber].PenColor,
  400.                   0);
  401.         SetROP2(hdc, SaveROP);
  402.         ReleaseDC(hWnd, hdc);
  403.     }
  404. }
  405.  
  406. /*********************************************************************
  407.  * function DoWMCommand
  408.  *    When a menu item is selected, this function changes the current
  409.  *    state of shape selections to match the user's menu selection.
  410.  *******************************************************************/
  411. int DoWMCommand(WPARAM wParam, HWND hWnd)
  412. {
  413.     switch(wParam)
  414.     {
  415.         case MID_QUIT:
  416.             DestroyWindow(hWnd);
  417.             break;
  418.  
  419.         case MID_LINE:
  420.             CurrentShape = LINE;
  421.             break;
  422.  
  423.         case MID_ELLIPSE:
  424.             CurrentShape = ELLIPSE;
  425.             break;
  426.  
  427.         case MID_RECTANGLE:
  428.             CurrentShape = RECTANGLE;
  429.             break;
  430.  
  431.         case MID_THIN:
  432.             PenWidth = 1;
  433.             break;
  434.  
  435.         case MID_REGULAR:
  436.             PenWidth = 3;
  437.             break;
  438.  
  439.         case MID_THICK:
  440.             PenWidth = 5;
  441.             break;
  442.  
  443.         case MID_RED:
  444.             PenColor = RGB(255, 0, 0);
  445.             break;
  446.  
  447.         case MID_GREEN:
  448.             PenColor = RGB(0, 255, 0);
  449.             break;
  450.  
  451.         case MID_BLACK:
  452.             PenColor = RGB(0, 0, 0);
  453.             break;
  454.  
  455.         default:
  456.             return 0;
  457.     }
  458.     return 1;
  459. }
  460.  
  461.